#ifndef __CUHB_UTILS_H__
#define __CUHB_UTILS_H__

/*
  Copyright (c) 2010 Davide Rossetti (davide.rossetti@roma1.infn.it)

  This file is part of CuHB (CUDA Heisenberg) package.
  
  CuHB is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.

  CuHB is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.
  
  You should have received a copy of the GNU General Public License
  along with CuHB.  If not, see <http://www.gnu.org/licenses/>.

*/

//-----------------------------------------------------------------------------

#define cast_as_hex(F) (*(unsigned int*)&(F))
    
//-----------------------------------------------------------------------------

#include <ieee754.h>
static inline bool is_nan(float f)
{
    ieee754_float w;
    w.f = f;
    return w.ieee_nan.exponent == 0xff;
}

//-----------------------------------------------------------------------------

#define error(FMT, ARGS...) do { fprintf(stdout,"[fatal %s:%d] " FMT, __FUNCTION__,  __LINE__, ##ARGS); exit(1); } while(0)

#define warn(FMT, ARGS...) do { fprintf(stdout,"[warning %s:%d] " FMT, __FUNCTION__,  __LINE__, ##ARGS); } while(0)

#define info(FMT, ARGS...) do { fprintf(stdout,"[info %s:%d] " FMT, __FUNCTION__,  __LINE__, ##ARGS); } while(0)

#if 0
#    define trace(FMT, ARGS...) do { fprintf(stdout, "[debug %s:%d] " FMT, __FUNCTION__,  __LINE__, ##ARGS); } while(0)
#else
#    warning "Tracing enabled"
#    define trace(...) do { } while(0)
#endif

#define cuda_verify(F)                                                  \
    do {                                                                \
        cudaError_t err = F;                                            \
        if(err != cudaSuccess)                                          \
            error("CUDA API error %d (%s)", err, cudaGetErrorString(err)); \
    } while(0)

//-----------------------------------------------------------------------------

/*
 * Local variables:
 *  mode: c++
 *  c-indent-level: 4
 *  c-basic-offset: 4
 *  tab-width: 4
 *  indent-tabs-mode: nil
 * End:
 */

#endif // __CUHB_UTILS_H__
